package com.easou.ps.master.kit;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

import com.easou.ps.common.config.Config;
import com.easou.ps.common.config.PropertiesConfig;

/**
 * Config access point
 * @author charles
 *
 */
public class ConfigFactory {
	
	static final Config SYS_CONFIG = new PropertiesConfig("sys");
	static final Config APP_CONFIG = new PropertiesConfig("app");
	
	static final ReentrantLock lock = new ReentrantLock();
	static final Map<String, Config> configMap = new HashMap<String, Config>();
	
	public static boolean IS_TEST = "test".equals(SYS_CONFIG.get("env"));
	
	/**
	 * system level configuration, such as ip, port, path, resource name, etc.
	 * @return
	 */
	public static Config getSysConfig() {
		return SYS_CONFIG;
	}
	
	/**
	 * app level configuration, such as strategy's parameters, threshold, etc.
	 * @return
	 */
	public static Config getAppConfig() {
		return APP_CONFIG;
	}
	
	public static Config getConfig(String confName) {
		lock.lock();
		try {
			Config config = configMap.get(confName);
			if (config == null) {
				config = new PropertiesConfig(confName);
				configMap.put(confName, config);
			}
			return config;
		} finally {
			lock.unlock();
		}
	}

}
